package com.koron.css2.clientCenter.impl;


import com.koron.css2.ServerInterface;
import com.koron.css2.clientCenter.Enum.ProcessStateEnum;
import com.koron.css2.clientCenter.bean.UserInfoMerge;
import com.koron.css2.clientCenter.bean.UserInfoMergeDetail;
import com.koron.css2.clientCenter.mapper.UserInfoMergeDetailMapper;
import com.koron.css2.clientCenter.mapper.UserInfoMergeMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.util.Constant;
import com.koron.util.JsonUtils;
import com.koron.util.Tools;
import org.koron.ebs.mybatis.SessionFactory;
import org.springframework.stereotype.Service;
import org.swan.bean.MessageBean;

import java.util.Date;
import java.util.List;

/**
 * 新增并户及并户详情数据
 *
 * @author 朱超
 * @since 2021-12-24 10:42:05
 */
@Service
public class UserInfoMergeInsert implements ServerInterface {

    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        try {
            UserInfoMergeMapper userInfoMergeMapper = factory.getMapper(UserInfoMergeMapper.class);
            UserInfoMergeDetailMapper userInfoMergeDetailMapper = factory.getMapper(UserInfoMergeDetailMapper.class);

            // 1. 根据请求json获取实例bean
            UserInfoMerge userInfoMerge = JsonUtils.objectToPojo(req.getData(), UserInfoMerge.class);
            List<UserInfoMergeDetail> userInfoMergeDetailList = userInfoMerge.getUserInfoMergeDetailList();
            // 2. 设置id，createAccount,createTime,createName
            userInfoMerge.setCreateInfo(userInfo);
            // 3. 设置billNo(需要在客户服务管理平台-基础配置-编码规则中配置)
            String billNo = Tools.getBillNo(factory, "user_info_merge", userInfo.getCurWaterCode());
            userInfoMerge.setBillNo(billNo);
            // 4. 设置billDate
            userInfoMerge.setBillDate(new Date());
            // 5. 设置processState，processName
            userInfoMerge.setProcessState(ProcessStateEnum.DRAFT.getState());
            userInfoMerge.setProcessName(ProcessStateEnum.DRAFT.getName());
            // 6. 设置mainCtmCode,mainCtmName
            for (UserInfoMergeDetail userInfoMergeDetail:userInfoMergeDetailList) {
                if (userInfoMergeDetail.getMainCtmFlag().equals(1)) {
                    userInfoMerge.setMainCtmCode(userInfoMergeDetail.getCtmNo());
                    userInfoMerge.setMainCtmName(userInfoMergeDetail.getCtmName());
                }
            }
            // 7. 新增并户数据
            userInfoMergeMapper.insert(userInfoMerge);
            // 8. 新增并户详情数据
            for (UserInfoMergeDetail userInfoMergeDetail : userInfoMergeDetailList) {
                // 8.1 设置id，createAccount,createTime,createName
                userInfoMergeDetail.setCreateInfo(userInfo);
                // 8.2 mergeCode
                userInfoMergeDetail.setMergeCode(billNo);
                // 8.3 插入数据
                userInfoMergeDetailMapper.insert(userInfoMergeDetail);
            }
            // 9. 返回id
//            MessageBean<String> msg = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "并户新增成功", String.class);
//            msg.setData(userInfoMerge.getId());
            MessageBean<UserInfoMerge> msg = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "并户新增成功", UserInfoMerge.class);
            msg.setData(userInfoMerge);

            return msg;
        } catch (Exception e) {
            logger.error("并户新增失败", e);
            return MessageBean.create(Constant.MESSAGE_INT_FAIL, "并户新增失败", void.class);
        }
    }

}
